package cn.com.enboga.musicbox.control
{
	import cn.com.enboga.musicbox.db.LocalDbConnection;
	import cn.com.enboga.musicbox.model.ModelLocator;
	import cn.com.enboga.musicbox.utils.OtherUtils;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.collections.ArrayCollection;
	
	public class MusicsListControl
	{
		//获得ModelLocator 实例
		internal var musicBox:ModelLocator = ModelLocator.getInstance();
		
		/**弹出目录选择*/
		public function addMp3ByDir():void{
			musicBox._file = new File();
			musicBox._file.addEventListener(Event.SELECT, dirSelected);
			musicBox._file.browseForDirectory("选择添加MP3目录");
		}
		
		/**将选择目录的文件添加到List*/
		private function dirSelected(e:Event):void{
			trace(musicBox._file.nativePath);
			var contents:Array = musicBox._file.getDirectoryListing();
			//musicBox.musicInfoList.removeAll();
			for(var i:uint=0; i<contents.length; i++){
				//trace(contents[i].name.toLocaleUpperCase());
				var nameUpp:String = contents[i].name.toLocaleUpperCase();
				var kzmStr:String = nameUpp.substr(nameUpp.length-4);
				if(kzmStr==".MP3"){
					musicBox._musicList.addItem(contents[i].name);
					musicBox._musicInfoList.addItem(contents[i]);
				}
			}
			
			savaM3u();
		}
		
		/**添加文件*/
		public function m3u():void{
			musicBox._musicList.removeAll();
			musicBox._musicInfoList.removeAll();
			
			var db:LocalDbConnection = new LocalDbConnection();
			//db.createMusicBoxInfo();
			//db.insertMusicBoxInfo();
			trace("文件:" + db.selectMusicBoxInfo());
			var file:File = File.applicationStorageDirectory.resolvePath(db.selectMusicBoxInfo());
			//Alert.show(File.applicationStorageDirectory.nativePath);
			var fileStream:FileStream = new FileStream();
			//fileStream.addEventListener(Event.COMPLETE, completed);
			fileStream.open(file, FileMode.READ);
			//var bytes:ByteArray = new ByteArray();
			var str:String = "";
			
			//function completed(event:Event):void{				
				str = fileStream.readMultiByte(fileStream.bytesAvailable, "gbk");
				fileStream.close();
				var contents:Array = str.split("\r\n");

				for(var i:uint=0; i<contents.length; i++){
					trace("文件: " + contents[i]);
					var nameUpp:String = contents[i].toLocaleUpperCase();
					if(nameUpp.substr(nameUpp.length-4) ==".MP3"){
						var temp:String = contents[i].toString();
						var index:int = temp.lastIndexOf("\\");
						musicBox._musicList.addItem(temp.substr(index+1, temp.length-1));
						var file:File = new File();
						file.nativePath = contents[i].toString();
						musicBox._musicInfoList.addItem(file);
					}
				}
				
				
			//}
		}
		
		/** 保存列表 */
		public function savaM3u():void{
			var db:LocalDbConnection = new LocalDbConnection();
			var file:File = File.applicationStorageDirectory.resolvePath(db.selectMusicBoxInfo());
			var fileStream:FileStream = new FileStream();
			trace(File.applicationStorageDirectory.nativePath);
			//Alert.show(File.applicationStorageDirectory.nativePath);
			fileStream.open(file, FileMode.WRITE);
			var str:String = "";

			for(var i:uint=0; i<musicBox._musicInfoList.length; i++){
				var fileTemp:File = musicBox._musicInfoList.getItemAt(i) as File;
				str += fileTemp.nativePath + "\r\n";
			}
			
			fileStream.writeMultiByte(str, "gbk");
			fileStream.close();
		}
		
		/** 将存储目录下的列表读取出来 */
		public function onLoadLists():void{
			var file:File = File.applicationStorageDirectory;
			var contents:Array = file.getDirectoryListing();
			
			for(var i:uint=0; i<contents.length; i++){
				var kzmStr:String = OtherUtils.getLastString(contents[i].name, ".").toLocaleUpperCase();
				//var nameUpp:String = contents[i].name.toLocaleUpperCase();
				//var kzmStr:String = nameUpp.substr(nameUpp.length-4);
				if(kzmStr == "M3U"){
					var musicList:ArrayCollection = new ArrayCollection();
					addM3uToList(file, musicList);
					musicBox._musicLists.addItem(musicList);
				}
			}
		}
		
		
		/** 将一个m3u歌曲信息添加到列表中 */
		public function addM3uToList(file:File, lists:ArrayCollection):void{			
			var fileStream:FileStream = new FileStream();
			fileStream.addEventListener(Event.COMPLETE, completed);
			fileStream.openAsync(file, FileMode.READ);
			
			function completed(event:Event):void{
				var str:String = fileStream.readMultiByte(fileStream.bytesAvailable, "gbk");
				fileStream.close();
				var contents:Array = str.split("\r\n");

				for(var i:uint=0; i<contents.length; i++){
					var kzmStr:String = OtherUtils.getLastString(contents[i], ".").toLocaleUpperCase();
					if(kzmStr == "MP3"){
						var musicInfo:Object = new Object();
						musicInfo.filePath = contents[i]; //设置文件路径
						musicInfo.fileName = OtherUtils.getLastString(contents[i], "\\").toLocaleUpperCase(); //文件名
						
						lists.addItem(musicInfo);
					}
				}				
			}
		}
	}
}